home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / Matrix44.cpp < prev    next >
C/C++ Source or Header  |  2005-11-18  |  7KB  |  286 lines

  1.  
  2. #include "Matrix44.h"
  3.  
  4. namespace peon
  5. {
  6.     Matrix44::Matrix44( float m0, float m4, float  m8, float m12,
  7.         float m1, float m5, float  m9, float m13,
  8.         float m2, float m6, float m10, float m14,
  9.         float m3, float m7, float m11, float m15 )
  10.     {
  11.         m[0]=m0; m[4]=m4; m[8] =m8;  m[12]=m12;
  12.         m[1]=m1; m[5]=m5; m[9] =m9;  m[13]=m13;
  13.         m[2]=m2; m[6]=m6; m[10]=m10; m[14]=m14;
  14.         m[3]=m3; m[7]=m7; m[11]=m11; m[15]=m15;
  15.     }
  16.  
  17.     void Matrix44::identity( void )
  18.     {
  19.         m[0]=1.0f; m[4]=0.0f; m[8] =0.0f; m[12]=0.0f;
  20.         m[1]=0.0f; m[5]=1.0f; m[9] =0.0f; m[13]=0.0f;
  21.         m[2]=0.0f; m[6]=0.0f; m[10]=1.0f; m[14]=0.0f;
  22.         m[3]=0.0f; m[7]=0.0f; m[11]=0.0f; m[15]=1.0f;
  23.     }
  24.  
  25.     void Matrix44::translate( const Vector3 &trans )
  26.     {
  27.         identity();
  28.  
  29.         m[12] = trans.x;
  30.         m[13] = trans.y;
  31.         m[14] = trans.z;
  32.     }
  33.  
  34.     void Matrix44::translate_x( const float &dist )
  35.     {
  36.         identity();
  37.  
  38.         m[12] = dist;
  39.     }
  40.  
  41.     void Matrix44::translate_y( const float &dist )
  42.     {
  43.         identity();
  44.  
  45.         m[13] = dist;
  46.     }
  47.  
  48.     void Matrix44::translate_z( const float &dist )
  49.     {
  50.         identity();
  51.  
  52.         m[14] = dist;
  53.     }
  54.  
  55.     void Matrix44::rotate( const float &angle, Vector3 &axis )
  56.     {
  57.         float s = sin(PEON_DEGTORAD(angle));
  58.         float c = cos(PEON_DEGTORAD(angle));
  59.  
  60.         axis.normalize();
  61.  
  62.         float ux = axis.x;
  63.         float uy = axis.y;
  64.         float uz = axis.z;
  65.  
  66.         m[0]  = c + (1-c) * ux;
  67.         m[1]  = (1-c) * ux*uy + s*uz;
  68.         m[2]  = (1-c) * ux*uz - s*uy;
  69.         m[3]  = 0;
  70.  
  71.         m[4]  = (1-c) * uy*ux - s*uz;
  72.         m[5]  = c + (1-c) * pow(uy,2);
  73.         m[6]  = (1-c) * uy*uz + s*ux;
  74.         m[7]  = 0;
  75.  
  76.         m[8]  = (1-c) * uz*ux + s*uy;
  77.         m[9]  = (1-c) * uz*uz - s*ux;
  78.         m[10] = c + (1-c) * pow(uz,2);
  79.         m[11] = 0;
  80.  
  81.         m[12] = 0;
  82.         m[13] = 0;
  83.         m[14] = 0;
  84.         m[15] = 1;
  85.     }
  86.  
  87.     void Matrix44::rotate_x( const float &angle )
  88.     {
  89.         float s = sin(PEON_DEGTORAD(angle));
  90.         float c = cos(PEON_DEGTORAD(angle));
  91.  
  92.         identity();
  93.  
  94.         m[5]  =  c;
  95.         m[6]  =  s;
  96.         m[9]  = -s;
  97.         m[10] =  c;
  98.     }
  99.  
  100.     void Matrix44::rotate_y( const float &angle )
  101.     {
  102.         float s = sin(PEON_DEGTORAD(angle));
  103.         float c = cos(PEON_DEGTORAD(angle));
  104.  
  105.         identity();
  106.  
  107.         m[0]  =  c;
  108.         m[2]  = -s;
  109.         m[8]  =  s;
  110.         m[10] =  c;
  111.     }
  112.  
  113.     void Matrix44::rotate_z( const float &angle )
  114.     {
  115.         float s = sin(PEON_DEGTORAD(angle));
  116.         float c = cos(PEON_DEGTORAD(angle));
  117.  
  118.         identity();
  119.  
  120.         m[0] =  c;
  121.         m[1] =  s;
  122.         m[4] = -s;
  123.         m[5] =  c;
  124.     }
  125.  
  126.     void Matrix44::scale( const Vector3 &scale )
  127.     {
  128.         identity();
  129.  
  130.         m[0]  = scale.x;
  131.         m[5]  = scale.y;
  132.         m[10] = scale.z;
  133.     }
  134.  
  135.     void Matrix44::transformPoint( Vector3 *vec )
  136.     {
  137.         Vector3 &v = *vec;
  138.  
  139.         float x = v.x;
  140.         float y = v.y;
  141.         float z = v.z;
  142.  
  143.         v.x = x * m[0] +
  144.             y * m[4] +
  145.             z * m[8] + m[12];
  146.  
  147.         v.y = x * m[1] +
  148.             y * m[5] +
  149.             z * m[9] + m[13];
  150.  
  151.         v.z = x * m[2] +
  152.             y * m[6] +
  153.             z * m[10]+ m[14];
  154.     }
  155.  
  156.     void Matrix44::transformVector( Vector3 *vec )
  157.     {
  158.         Vector3 &v = *vec;
  159.  
  160.         float x = v.x;
  161.         float y = v.y;
  162.         float z = v.z;
  163.  
  164.         v.x = x * m[0] +
  165.             y * m[4] +
  166.             z * m[8];
  167.  
  168.         v.y = x * m[1] +
  169.             y * m[5] +
  170.             z * m[9];
  171.  
  172.         v.z = x * m[2] +
  173.             y * m[6] +
  174.             z * m[10];
  175.     }
  176.  
  177.     Matrix44 Matrix44::operator + ( const Matrix44 &other )
  178.     {
  179.         Matrix44 result;
  180.  
  181.         result.m[0]  = m[0]  + other.m[0];
  182.         result.m[1]  = m[1]  + other.m[1];
  183.         result.m[2]  = m[2]  + other.m[2];
  184.         result.m[3]  = m[3]  + other.m[3];
  185.  
  186.         result.m[4]  = m[4]  + other.m[4];
  187.         result.m[5]  = m[5]  + other.m[5];
  188.         result.m[6]  = m[6]  + other.m[6];
  189.         result.m[7]  = m[7]  + other.m[7];
  190.  
  191.         result.m[8]  = m[8]  + other.m[8];
  192.         result.m[9]  = m[9]  + other.m[9];
  193.         result.m[10] = m[10] + other.m[10];
  194.         result.m[11] = m[11] + other.m[11];
  195.  
  196.         result.m[12] = m[12] + other.m[12];
  197.         result.m[13] = m[13] + other.m[13];
  198.         result.m[14] = m[14] + other.m[14];
  199.         result.m[15] = m[15] + other.m[15];
  200.  
  201.         return result;
  202.     }
  203.  
  204.     Matrix44 Matrix44::operator - ( const Matrix44 &other )
  205.     {
  206.         Matrix44 result;
  207.  
  208.         result.m[0]  = m[0]  - other.m[0];
  209.         result.m[1]  = m[1]  - other.m[1];
  210.         result.m[2]  = m[2]  - other.m[2];
  211.         result.m[3]  = m[3]  - other.m[3];
  212.  
  213.         result.m[4]  = m[4]  - other.m[4];
  214.         result.m[5]  = m[5]  - other.m[5];
  215.         result.m[6]  = m[6]  - other.m[6];
  216.         result.m[7]  = m[7]  - other.m[7];
  217.  
  218.         result.m[8]  = m[8]  - other.m[8];
  219.         result.m[9]  = m[9]  - other.m[9];
  220.         result.m[10] = m[10] - other.m[10];
  221.         result.m[11] = m[11] - other.m[11];
  222.  
  223.         result.m[12] = m[12] - other.m[12];
  224.         result.m[13] = m[13] - other.m[13];
  225.         result.m[14] = m[14] - other.m[14];
  226.         result.m[15] = m[15] - other.m[15];
  227.  
  228.         return result;
  229.     }
  230.  
  231.     Matrix44 Matrix44::operator * ( const Matrix44 &other )
  232.     {
  233.         Matrix44 result;
  234.  
  235.         result.m[0]  = (m[0]*other.m[0])+(m[4]*other.m[1])+(m[8]*other.m[2])+(m[12]*other.m[3]);
  236.         result.m[1]  = (m[1]*other.m[0])+(m[5]*other.m[1])+(m[9]*other.m[2])+(m[13]*other.m[3]);
  237.         result.m[2]  = (m[2]*other.m[0])+(m[6]*other.m[1])+(m[10]*other.m[2])+(m[14]*other.m[3]);
  238.         result.m[3]  = (m[3]*other.m[0])+(m[7]*other.m[1])+(m[11]*other.m[2])+(m[15]*other.m[3]);
  239.  
  240.         result.m[4]  = (m[0]*other.m[4])+(m[4]*other.m[5])+(m[8]*other.m[6])+(m[12]*other.m[7]);
  241.         result.m[5]  = (m[1]*other.m[4])+(m[5]*other.m[5])+(m[9]*other.m[6])+(m[13]*other.m[7]);
  242.         result.m[6]  = (m[2]*other.m[4])+(m[6]*other.m[5])+(m[10]*other.m[6])+(m[14]*other.m[7]);
  243.         result.m[7]  = (m[3]*other.m[4])+(m[7]*other.m[5])+(m[11]*other.m[6])+(m[15]*other.m[7]);
  244.  
  245.         result.m[8]  = (m[0]*other.m[8])+(m[4]*other.m[9])+(m[8]*other.m[10])+(m[12]*other.m[11]);
  246.         result.m[9]  = (m[1]*other.m[8])+(m[5]*other.m[9])+(m[9]*other.m[10])+(m[13]*other.m[11]);
  247.         result.m[10] = (m[2]*other.m[8])+(m[6]*other.m[9])+(m[10]*other.m[10])+(m[14]*other.m[11]);
  248.         result.m[11] = (m[3]*other.m[8])+(m[7]*other.m[9])+(m[11]*other.m[10])+(m[15]*other.m[11]);
  249.  
  250.         result.m[12] = (m[0]*other.m[12])+(m[4]*other.m[13])+(m[8]*other.m[14])+(m[12]*other.m[15]);
  251.         result.m[13] = (m[1]*other.m[12])+(m[5]*other.m[13])+(m[9]*other.m[14])+(m[13]*other.m[15]);
  252.         result.m[14] = (m[2]*other.m[12])+(m[6]*other.m[13])+(m[10]*other.m[14])+(m[14]*other.m[15]);
  253.         result.m[15] = (m[3]*other.m[12])+(m[7]*other.m[13])+(m[11]*other.m[14])+(m[15]*other.m[15]);
  254.  
  255.         return result;
  256.     }
  257.  
  258.     Matrix44 Matrix44::operator * ( const float scalar )
  259.     {
  260.         Matrix44 result;
  261.  
  262.         result.m[0]  = m[0]  * scalar;
  263.         result.m[1]  = m[1]  * scalar;
  264.         result.m[2]  = m[2]  * scalar;
  265.         result.m[3]  = m[3]  * scalar;
  266.  
  267.         result.m[4]  = m[4]  * scalar;
  268.         result.m[5]  = m[5]  * scalar;
  269.         result.m[6]  = m[6]  * scalar;
  270.         result.m[7]  = m[7]  * scalar;
  271.  
  272.         result.m[8]  = m[8]  * scalar;
  273.         result.m[9]  = m[9]  * scalar;
  274.         result.m[10] = m[10] * scalar;
  275.         result.m[11] = m[11] * scalar;
  276.  
  277.         result.m[12] = m[12] * scalar;
  278.         result.m[13] = m[13] * scalar;
  279.         result.m[14] = m[14] * scalar;
  280.         result.m[15] = m[15] * scalar;
  281.  
  282.         return result;
  283.     }
  284. }
  285.  
  286.